/*Quindel-las-pelotas*/
 
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
//import javax.microedition.io.file.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
public class ImageViewer extends MIDlet implements CommandListener 
{
	private String currDirName;
 
	/*screen commands*/
	private Command view = new Command("View", Command.ITEM, 1);
	private Command back = new Command("Back", Command.BACK, 2);
	private Command exit = new Command("Exit", Command.EXIT, 3);
	private Command backfromimage;
	private Command zoom;
	private Command zoomin;
	private Command zoomout;
	private Command rotate;
 
	private final static String UP_DIRECTORY = "/";
	private final static String MEGA_ROOT = "/";
	private final static String SEP_STR = "/";
	private final static char   SEP = '/';


	
	private ImageItem imageItem;
	private Image start;
	private Display display;
	private Form form;
	private ScrollCanvas image;
	List browser; 
	private int zoomlevel=1;
	boolean zoomf=false;
	boolean rotatef;
	
 
    public ImageViewer() 
    {
        System.out.println("here1");
        currDirName = MEGA_ROOT;
    }
 
    public void startApp() 
    {


        boolean isAPIAvailable = false;
        if (System.getProperty("microedition.io.file.FileConnection.version") != null)
        {
            isAPIAvailable = true;
            showFile();
            System.out.println("hi");
            try 
            {
                //showCurrDir();
                //showFile();
                System.out.println("h5");
            } 
            catch (SecurityException e) 
            {
                System.out.println(e);
            } 
            catch (Exception e) {System.out.println(e);}
        }
        else
        {
            StringBuffer splashText = new StringBuffer
            (getAppProperty("MIDlet-Name")).append("\n").append(
            getAppProperty("MIDlet-Vendor")).
            append(isAPIAvailable?"":"\nFileConnection API not available");
            Alert splashScreen = new Alert(null,splashText.toString(),
            null,AlertType.INFO);
            splashScreen.setTimeout(3000);
            Display.getDisplay(this).setCurrent(splashScreen);
        }
    
    }
 
    public void pauseApp() {}
 
    public void destroyApp(boolean cond) 
    {
        notifyDestroyed();
    }
 
    public void commandAction(Command c, Displayable d) 
    {
        System.out.println("updir:"+UP_DIRECTORY);
        if (c == view) 
        {
            List curr = (List)d;
            final String currFile = curr.getString(curr.getSelectedIndex());
            System.out.println("currFile:"+currFile);
            
            new Thread(new Runnable() 
            {
                public void run() 
                {
                    
                        showFile();
                    
			
                }
            }                       ).start();
		System.out.println("thread end");
        }
        else if (c == back) 
        {

            showCurrDir();
        } 
	else if (c == rotate) 
        {
		//image.rotateImage();
	}
	else if (c == zoom) 
        {
		
		zoomf=true;
		zoomin = new Command("+", Command.SCREEN, 1);
		zoomout = new Command("-", Command.OK, 1);
		image.removeCommand(zoom);
		image.removeCommand(exit);
		//image.removeCommand(backfromimage);
		image.removeCommand(rotate);
		image.addCommand(zoomin);
		image.addCommand(zoomout);
		//image.rescaleImage(20,20);
        } 
	else if (c == zoomin) 
        {

		if(zoomlevel!=1)zoomlevel--;
		//image.rescaleImage(zoomlevel);
        } 
	else if (c == zoomout) 
        {

		if(zoomlevel!=5)zoomlevel++;
		//image.rescaleImage(zoomlevel);
        } 
	else if (c == backfromimage) 
        {
		//image.finalize();
		
		if(zoomf){
			image.removeCommand(zoomin);
			image.removeCommand(zoomout);
			image.addCommand(zoom);
			image.addCommand(rotate);
			zoomf=false;

		}else{
			Display.getDisplay(this).setCurrent(browser);
		}
		//currDirName = MEGA_ROOT;
            //showCurrDir();
        } 
        else if (c == exit) 
        {
            destroyApp(false);
        }
    }
 
    void showCurrDir() 
    {
			    //    List browser;        
	//Thread t = new Thread() {
        //public void run() {
	Enumeration e;
//        FileConnection currDir = null;




//	}	//THREAD 
  //    }; 
    //  t.start();     
	           
		            
    }
  
    void traverseDirectory(String fileName) 
    {
        System.out.println("fileName:"+fileName+"cur_dir:"+currDirName+"mega_root:"+MEGA_ROOT);
        if (currDirName.equals(MEGA_ROOT)) 
        {
            if (fileName.equals(UP_DIRECTORY)) 
            {
                // can not go up from MEGA_ROOT
                return;
            }
            currDirName = fileName;
        } 
        else if (fileName.equals(UP_DIRECTORY)) 
        {
            System.out.println("up");
            // Go up one directory
            // TODO use setFileConnection when implemented
            int i = currDirName.lastIndexOf(SEP, currDirName.length()-2);
            if (i != -1) 
            {
                currDirName = currDirName.substring(0, i+1);
            } 
            else 
            {
                currDirName = MEGA_ROOT;
            }
        } 
        else 
        {
            currDirName = currDirName + fileName;
        }
        showCurrDir();
    }
  
    void showFile() 
    {
        
		display = Display.getDisplay(this);


		//backfromimage = new Command("Back", Command.BACK, 1);
		//rotate = new Command("Rotate", Command.SCREEN, 1);
		//zoom = new Command("Zoom", Command.SCREEN, 2);


		image = new ScrollCanvas(this);

		//browser.removeCommand(exit);
		//image.addCommand(zoom);
		//image.addCommand(rotate);
		//image.addCommand(backfromimage);
		//image.setCommandListener(this);
		display.setCurrent(image); 
    }


} 
 
 

